<?php

namespace App;

use App\Models\Productline;
use App\Models\ProductLineMember;
use App\Models\Role;
use App\Models\SignInLog;
use App\Models\Topic;
use App\Models\UserAddress;
use App\Models\UserGroup;
use Carbon\Carbon;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
use App\Models\Admin;
use App\Models\Score;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'hawk_user';

    const CREATED_AT = 'regtime';
    const UPDATED_AT = null;
    protected $dateFormat = 'U';

    public static $sexText = [
        1 => '男',
        2 => '女',
    ];

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'password', 'phone', 'avatar', 'regtime', 'sex', 'realName', 'briefIntro'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getSexTextAttribute()
    {
        return self::$sexText[$this->sex] ?? '';
    }

    /**
     * 员工的角色
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'hawk_userrole', 'userId', 'roleId');
    }

    /**
     * 员工所属产生线
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function productlines()
    {
        return $this->belongsToMany(Productline::class, 'hawk_pdlmember', 'userId', 'productlineId')
            ->using(ProductLineMember::class)
            ->withTimestamps('addTime', 'updateTime');
    }

    /**
     * 加入的班组
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function groups()
    {
        return $this->belongsToMany(UserGroup::class, 'hawk_groupmember', 'userId', 'groupId');
    }

}
